package com.dt.common.validation.definition;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.dt.common.validation.annotation.Option;
import com.dt.common.validation.impl.DefaultValidationDefinition;
import com.dt.common.validation.itf.ConstraintValidation;
import com.dt.common.validation.itf.ValidationContext;

public class OptionDefinition extends DefaultValidationDefinition {
	private Set<String> valueSet;

	public OptionDefinition() {
		this.name = Option.class.getSimpleName();
	}

	public void setValues(List<String> values) {
		this.valueSet = new HashSet<>(values);
	}

	@Override
	public ConstraintValidation buildValidation() {
		final Set<String> valueSet_ = valueSet;
		return new ConstraintValidation() {

			@Override
			public boolean isValid(ValidationContext context, Object objectValue, Object propertyValue) {
				if (propertyValue == null) {
					return true;
				}
				String strValue = propertyValue.toString();
				return valueSet_.contains(strValue);
			}
		};

	}

}
